package com.huawei.spare.part.replace.rx;

import com.huawei.spare.part.replace.models.DownloadTask;
import javafx.beans.binding.Bindings;
import javafx.beans.binding.BooleanBinding;
import javafx.beans.property.SimpleObjectProperty;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;

public class ObservarList {

    public static void main(String[] args) {

        final SimpleObjectProperty<DownloadTask.Status> status1 = new SimpleObjectProperty<>(DownloadTask.Status.Pending);
        final SimpleObjectProperty<DownloadTask.Status> status2 = new SimpleObjectProperty<>(DownloadTask.Status.Pending);
        final ObservableList<SimpleObjectProperty<DownloadTask.Status>> list = FXCollections.observableArrayList(
                status1,
                status2
        );


        final boolean b = list.stream().allMatch(status -> status.get().equals(DownloadTask.Status.Completed));


        final BooleanBinding booleanBinding = new BooleanBinding() {
            {
                list.forEach((status) -> {
                    this.bind(status);
                });

            }
            @Override
            protected boolean computeValue() {
                return list.stream().allMatch(status -> status.isEqualTo(DownloadTask.Status.Completed).get());
            }
        };
        booleanBinding.addListener(new ChangeListener<Boolean>() {
            @Override
            public void changed(ObservableValue<? extends Boolean> observable, Boolean oldValue, Boolean newValue) {
                System.out.println("binding: " + newValue);
            }
        });

        status1.set(DownloadTask.Status.Completed);
        status2.set(DownloadTask.Status.Completed);

        status1.set(DownloadTask.Status.Pending);
        status2.set(DownloadTask.Status.Pending);

        status1.set(DownloadTask.Status.Completed);
        status2.set(DownloadTask.Status.Completed);

    }
}
